const { imgUrl } = getApp().globalData;
import { mineApi } from "@/utils/request/api/mine";
import { publicApi } from "@/utils/request/api/public";
Page({
  data: {
    type: 'add',
    imgUrl: imgUrl + "mine/",
    isMore: false,
    showPre: false,
    license: {
      name: "", //证书名称
      issuing_authority: "", //发证机关
      expire_date: "", //到期时间
      file_url: "", //证书文件
    },
    units: ["单位1", "单位2", "单位3", "单位4"],
  },
  change(event) {
    let license = JSON.parse(JSON.stringify(this.data.license));
    license[event.currentTarget.dataset.type] = event.detail.value;
    this.setData({
      license,
    });
  },
  onClose() {
    this.setData({
      showPre: false,
    })
  },
  showMore() {
    this.setData({
      isMore: true,
    })
  },
  hideMore() {
    this.setData({
      isMore: false,
    })
  },
  select(event) {
    const {
      currentTarget: {
        dataset: { type, origin },
      },
    } = event;
    let license = JSON.parse(JSON.stringify(this.data.license));
    license[type] = this.data[origin][event.detail.value / 1];
    this.setData({
      license,
    });
  },
  selectDate(event) {
    let license = JSON.parse(JSON.stringify(this.data.license));
    license.expire_date = event.detail.value;
    this.setData({
      license,
    });
  },
  chooseImage(event) {
    const that = this;
    wx.chooseMessageFile({
      count: 1,
      type: "file",
      extension: ["pdf", "docx", "jpg", "png", "jpeg"],
      success: function (res) {
        const filePath = res.tempFiles[0].path;
        const formData = {
          fileVal: filePath,
          name: res.tempFiles[0].name,
        };

        publicApi
          .uploadFile({
            filePath,
            formData,
          })
          .then((res) => {
            let obj = JSON.parse(JSON.stringify(that.data.license))
            obj.file_url = res.data 
            that.setData({
              license: obj
            })
          });
      },
    });
  },
  confirmAdd() {
    let license = JSON.parse(JSON.stringify(this.data.license));
    if (
      license.name &&
      license.issuing_authority &&
      license.expire_date &&
      license.file_url
    ) {
      Object.assign(license, {
        company_id: wx.getStorageSync('company_id'),
        issuing_date: '2025-01-01',
        is_expire: false,
      })
      mineApi
        .addCompanyZs(license)
        .then((res) => {
          wx.showToast({
            title: "添加成功",
            icon: "success",
            duration: 2000,
            success: function () {
              setTimeout(function () {
                wx.navigateBack({
                  delta: 1,
                });
              }, 2000);
            }
          })
        })
    } else {
      wx.showToast({
        title: "请完善信息",
        icon: "none",
        duration: 2000,
      })
    }
  },
  cancelAdd() {
    wx.navigateBack({
      delta: 1,
    });
  },
  handlePreImg() {
    this.setData({
      showPre: true
    })
  },
  afterRead() {},
  onLoad(options) {
    const eventChannel = this.getOpenerEventChannel();
    eventChannel.on('edit', data => {
      this.setData({
        license: data.data,
        type: 'edit'
      })
    });
  },
  onReady() {},
  onShow() {},
  onHide() {},
  onUnload() {},
  onShareAppMessage() {
    return {
      title: "",
    };
  },
});
